package com.it.spider;

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.it.domain.School;
import com.it.utils.MyBatisUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.ibatis.session.SqlSession;


import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/16.
 */
public class JoupTest {

    public static void main(String[] args) {
//        String url = "";
//        try {
////            Connection.Response response = Jsoup.connect(url).timeout(60000).execute();
////            Map<String, String> cookie = response.cookies();
//////            for (String key : cookie.keySet()) {
//////                System.out.println(key + "==" + cookie.get(key));
//////            }
//////            System.out.println(response.body());
////            response.
//
//            url = "http://www.tizi.com/class/schools/county_sch";
//            Map<String, String> param = new HashMap<>();
//            param.put("id", "500");
//            param.put("sctype", "1");
//            param.put("token", "2bdeb9da55af818b9c7712ab8918430e");
//            param.put("ver", "1450192902443");
//            param.put("page_name", "1492a797df4708f6c25bca7a683599b8");
//
//            Map<String, String> cookie = new HashMap<>();
//            param.put("TZID", "1b794282ba9913cfdd2508fd12ffe879");
//            param.put("uid", "eccUO1ZwJimoPgM4JTYEAg==");
//
//            Document document = Jsoup.connect(url).data(param).post();
////            for (String key : cookie.keySet()) {
////                System.out.println(key + "==" + cookie.get(key));
////            }
//            System.out.println(document.text());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        HtmlPage scorePage = null;
        WebClient webclient = null;
        webclient = new WebClient(BrowserVersion.CHROME);
        webclient.setAjaxController(new NicelyResynchronizingAjaxController());
        WebClientOptions options = webclient.getOptions();
        webclient.waitForBackgroundJavaScript(1000);
        options.setJavaScriptEnabled(true);
        options.setRedirectEnabled(true);
        options.setActiveXNative(false);
        options.setAppletEnabled(false);
        options.setCssEnabled(false);
        options.setDoNotTrackEnabled(false);
        options.setGeolocationEnabled(false);
        options.setUseInsecureSSL(true);
        options.setThrowExceptionOnFailingStatusCode(false);
        options.setThrowExceptionOnScriptError(false);

        try {
//            HtmlPage resultPage = webclient.getPage("http://www.tizi.com/class/area?id=2");
//            HtmlAnchor anchor = resultPage.getAnchorByText("点击这里选择学校");
//            System.out.println(anchor.asXml());
//            resultPage = anchor.click();
//            System.out.println(resultPage.asXml());
            HtmlPage resultPage = null;
//            resultPage = webclient.getPage("http://www.tizi.com/class/schools/county_sch?id=500&sctype=1&token=2bdeb9da55af818b9c7712ab8918430e&page_name=1492a797df4708f6c25bca7a683599b8&v=1450192902443");
            WebRequest request = new WebRequest(new URL("http://www.tizi.com/class/schools/county_sch"));
//            request.setAdditionalHeader("Referer", "http://www.tizi.com/banji");
            request.setHttpMethod(HttpMethod.POST);

            List<NameValuePair> requestParameters = new ArrayList();
            requestParameters.add(new NameValuePair("id", "778"));
            requestParameters.add(new NameValuePair("sctype", "2"));//2bdeb9da55af818b9c7712ab8918430e
            requestParameters.add(new NameValuePair("token", "3b024325646442a61a62301e3bf38fa5"));
            requestParameters.add(new NameValuePair("page_name", "1492a797df4708f6c25bca7a683599b8"));
            requestParameters.add(new NameValuePair("v", "1450192902443"));
            request.setRequestParameters(requestParameters);

            Map<String, String> head = new HashMap();
            head.put("Cookie", "TZID=2bde16d0e0275b3db9947ef628f14acb;uid=eccUO1ZxZKqoWwM5JbLAAg==;");
            head.put("Referer", "http://www.tizi.com/banji");
            head.put("Host", "www.tizi.com");
            request.setAdditionalHeaders(head);
            resultPage = webclient.getPage(request);
            System.out.println(resultPage.asXml());

            String result =resultPage.asText().toString();
            JSONObject schoolJson =JSONObject.fromObject(result);
                String data = schoolJson.getString("data");
            JSONObject dataJson= JSONObject.fromObject(data);
            System.out.println(dataJson);
            for(int i=0; i<dataJson.size(); i++){
                for (int j=97;j<=122;j++) {
                    String code= null;
                   try {
                       code = dataJson.getString((char)j +"");
                   }catch(JSONException e){
                        System.out.println((char)j+"   开头学校无");
                    }
                    if(code==null) {
                        continue;

                    }
                    JSONArray  schoolArray= JSONArray.fromObject(code);

                    for(int z = 0 ;z<schoolArray.size();z++){

                        String id =schoolArray.getJSONObject(z).getString("id");
                        String name =schoolArray.getJSONObject(z).getString("schoolname");
                       /* String  =schoolArray.getJSONObject(z).getString("id");
                        String id =schoolArray.getJSONObject(z).getString("id");
                        String id =schoolArray.getJSONObject(z).getString("id");*/
                                System.out.println("ID"+ id +"   "+name);
                        School school =new School();
                        school.setSchool_name(name);
                        //school.setSchool_area("广东省茂名市化州市");
                        school.setSchool_type("公立中学");
                        add(school);
                    }
                }
            }




        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public static void add(School school){
        //SqlSession sqlSession = MyBatisUtil.getSqlSession(false);
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        /**
         * 映射sql的标识字符串，
         * cn.it.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值，
         * addUser是insert标签的id属性值，通过insert标签的id属性值就可以找到要执行的SQL
         */

        String statement = "com.it.mapping.schoolMapper.addSchool";//映射sql的标识字符串

        //执行插入操作
        int retResult = sqlSession.insert(statement,school);

        //手动提交事务
        //sqlSession.commit();
        //使用SqlSession执行完SQL之后需要关闭SqlSession
        sqlSession.close();
        System.out.println(retResult);
    }
}
